<?php

namespace app\api\controller;

use app\model\User as UserModel;
use app\model\Enter;
use app\model\Collect;
use app\model\WorkHour;
use app\model\Staff;
use app\common\file\ExcelService;
use think\facade\Env;
use app\model\OnJob;
use app\model\Task;
use app\model\WorkTag;

class User extends Auth
{
    /**
     * 个人资料
     *
     * @return void
     */
    public function info()
    {
        $user_id = $this->getUserId();
        $info = UserModel::getUserInfo($user_id);
        success('个人资料', $info);
    }

    /**
     * 待结算金额
     *
     * @return void
     */
    public function waitPaymenMoney()
    {
        $list = UserModel::waitPaymenMoney($this->getUserId());
        success('待结算金额', $list);
    }

    /**
     * 本月工时
     *
     * @return void
     */
    public function  workHour()
    {
        $time = input('time');
        if (empty($time)) {
            $time = strtotime(date('Y-m-1', time()));
        } else {
            $time = strtotime(date('Y-m-1', strtotime($time)));
        }
        $is_download = input('is_download');

        $id_num = Staff::where('boss_id', $this->getUserId())->column('id_num');
        $list = WorkHour::getList($time, $id_num);

        if (!empty($is_download)) {
            $list = WorkHour::whereIn('id_num', $id_num)->where('time', '=', $time)->select()->toArray();
            $path = ExcelService::exportExcel(date('Y-m', $time) . '月工时表' . $this->getUserId() . date('YmdHis', time()), $list, 1);
            success('工时表下载地址', Env::get('FILE.PATH') . 'exportExcel/' . $path);
        }

        success('工时列表', $list);
    }

    /**
     * 在职人员列表
     *
     * @return void
     */
    public function onJobList()
    {
        $user_id = $this->getUserId();
        $time = input('time');
        $list = Staff::getonJobList($user_id, $time);

        $is_download = input('is_download');
        if (!empty($is_download)) {
            $staff_id = Staff::where('boss_id', $this->getUserId())->column('id');
            // 确定入职时间
            if (!empty($time)) {
                $y = date('Y', strtotime($time));
                $m = date('m', strtotime($time));
            } else {
                $y = date('Y', time());
                $m = date('m', time());
            }

            $time = mFristAndLast($y, $m);
            $start_time = date('Y-m-d H:i:s', $time['firstday']);
            $end_time = date('Y-m-d H:i:s', $time['lastday']);

            $where[] = ['entry_time', 'between', "{$start_time}, {$end_time}"];

            $list = OnJob::where($where)
                ->field('id, name, id_num, entry_time, task_id')
                ->select()->toArray();
            foreach ($list as $k => $v) {
                $task_info = Task::where('id', $v['task_id'])->find();
                $list[$k]['company_name'] = $task_info['company_name'];
                $list[$k]['address_sheng'] = $task_info['address_sheng'];
                $list[$k]['address_shi'] = $task_info['address_shi'];
                $list[$k]['address_details'] = $task_info['address_details'];
            }
            $path = ExcelService::exportExcel(date('Y-m', $time['firstday']) . '在职人员列表' . $this->getUserId() . date('YmdHis', time()), $list, 2);
            success('在职人员列表下载地址', Env::get('FILE.PATH') . 'exportExcel/' . $path);
        }
        success('在职人员列表', $list);
    }

    /**
     * 离职人员列表
     *
     * @return void
     */
    public function leaveJobList()
    {
        $user_id = $this->getUserId();
        $time = input('time');
        $list = Staff::leaveJobList($user_id, $time);
        $is_download = input('is_download');
        if (!empty($is_download)) {
            $where[] = ['type', '=', 3];
            $staff_id = Staff::where('boss_id', $this->getUserId())->column('id');
            $where[] = ['staff_id', 'in', $staff_id];
            // 确定入职时间
            if (!empty($time)) {
                $y = date('Y', strtotime($time));
                $m = date('m', strtotime($time));
            } else {
                $y = date('Y', time());
                $m = date('m', time());
            }

            $time = mFristAndLast($y, $m);
            $start_time = date('Y-m-d H:i:s', $time['firstday']);
            $end_time = date('Y-m-d H:i:s', $time['lastday']);

            $where[] = ['entry_time', 'between', "{$start_time}, {$end_time}"];

            $list = OnJob::where($where)
                ->field('id, name, id_num, phone, leave_time, cause')
                ->select()
                ->toArray();
            $path = ExcelService::exportExcel(date('Y-m', $time['firstday']) . '离职人员列表' . $this->getUserId() . date('YmdHis', time()), $list, 3);
            success('离职人员列表下载地址', Env::get('FILE.PATH') . 'exportExcel/' . $path);
        }
        success('离职人员列表', $list);
    }

    /**
     * 待入职人员列表
     *
     * @return void
     */
    public function waitOnJobList()
    {
        $user_id = $this->getUserId();
        $list = Staff::waitOnJobList($user_id);

        $is_download = input('is_download');
        if (!empty($is_download)) {
            $staff_id = OnJob::where('type', 1)->column('staff_id');
            $list = Staff::field('id, name, sex, age, id_num, phone, create_time, expect_work, before_work, compensation, is_ill')
                ->where('boss_id', $this->getUserId())
                ->whereNotIn('id', $staff_id)
                ->withAttr('expect_work', function($value, $data) {
                    return json_decode($value);
                })
                ->select()
                ->toArray();
            foreach ($list as $k => $v) {
                $list[$k]['expect_work_name'] = WorkTag::whereIn('id', $v['expect_work'])->column('tag');
            }
            $path = ExcelService::exportExcel('待职人员列表' . $this->getUserId() . date('YmdHis', time()), $list, 4);
            success('待职人员列表下载地址', Env::get('FILE.PATH') . 'exportExcel/' . $path);
        }

        success('待入职人员列表', $list);
    }

    /**
     * 修改资料
     *
     * @return void
     */
    public function updateUserInfo()
    {
        $user_id = $this->getUserId();
        $update_user_info_data = ['attr' => input('attr')];
        UserModel::updateUserInfo($user_id, $update_user_info_data);
        success('修改成功');
    }

    /**
     * 代办事项
     *
     * @return void
     */
    public function toDo()
    {
        $user_id = $this->getUserId();
        $list = Enter::toDo($user_id);
        success('代办事项', $list);
    }

    /**
     * 通过、驳回入驻
     *
     * @return void
     */
    public function setType()
    {
        $user_id = $this->getUserId();
        $data = [
            'id'    => input('id'),
            'type'  => input('type'),
            'cause' => input('cause')
        ];

        if (empty($data['id']) || empty($data['type'])) {
            error('缺少参数');
        }

        $res = Enter::setType($user_id, $data);
        success($res);
    }

    /**
     * 族群
     *
     * @return void
     */
    public function group()
    {
        $user_id = $this->getUserId();
        $group = UserModel::getGroup($user_id);
        success('族群', $group);
    }

    /**
     * 获取收藏列表
     *
     * @return void
     */
    public function getCollectList()
    {
        $list = Collect::getList($this->getUserId());
        success('收藏列表', $list);
    }
}
